import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:largefriends/pages/lf_base_controller.dart';
import 'package:largefriends/pages/lf_blocked_members/lf_blocked_members_logic.dart';
import 'package:largefriends/res/lf_color.dart';
import 'package:largefriends/res/lf_double_ext.dart';
import 'package:largefriends/res/lf_text_style.dart';
import 'package:largefriends/res/strings/lf_strings.dart';
import 'package:largefriends/service/lf_auth_service.dart';
import 'package:largefriends/widgets/common_empty_page.dart';
import 'package:largefriends/widgets/common_failed_page.dart';
import 'package:largefriends/widgets/lf_app_bar.dart';

import 'lf_blocked_loading.dart';
import 'lf_blocked_members_widget.dart';

class LfBlockedMembersPage extends StatefulWidget {
  const LfBlockedMembersPage({super.key});

  @override
  State<LfBlockedMembersPage> createState() => _LfBlockedMembersPageState();
}

class _LfBlockedMembersPageState extends State<LfBlockedMembersPage> {
  final logic = Get.put(LfBlockedMembersLogic());
  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      child: Scaffold(
        appBar: lfAppBar(
          title: strBlockedMembers.tr,
          leadingCallback: () {
            Get.back();
          },
        ),
        body: GetBuilder<LfBlockedMembersLogic>(
          builder: (controller) {
            LfAuthService authService = Get.find();
            if (controller.dataList.isEmpty) {
              if (controller.state == LoadingState.loading ||
                  controller.state == LoadingState.init) {
                return const LfBlockedLoading();
              } else if (controller.state == LoadingState.failed) {
                return CommonFailedPage(retry: () {
                  controller.getCustomParamHttpRequest(isRefresh: true);
                });
              }
              return LayoutBuilder(
                builder: (context, constraints) {
                  return CommonEmptyPage(
                    title: strNoBlockedMembers.tr,
                    tip: strNoBlockedMembersTip.tr,
                    constraints: constraints,
                  );
                },
              );
            }
            return Padding(
              padding: EdgeInsets.symmetric(horizontal: 10.w),
              child: EasyRefresh(
                controller: controller.refreshController,
                child: CustomScrollView(
                  slivers: [
                    Text(
                      strBlockedMembersTip.tr,
                      style: lfTextStyle(sp: 15.sp),
                    ).sliverBox,
                    10.h.sbh.sliverBox,
                    ...controller.dataList.map(
                      (e) => LfBlockedMembersWidget(
                        userEntity: e,
                        click: () {
                          controller.unblock(e);
                        },
                      ).sliverBox,
                    ),
                  ],
                ),
                onLoad: () async {
                  var result = await controller.getCustomParamHttpRequest(
                      isRefresh: false);
                  return result;
                },
                onRefresh: () async {
                  var result =
                      controller.getCustomParamHttpRequest(isRefresh: true);
                  return result;
                },
              ),
            );
          },
        ),
      ),
      onWillPop: () async {
        Get.back();
        return false;
      },
    );
  }
}
